/*
    openaltimeter -- an open-source altimeter for RC aircraft
    Copyright (C) 2010  Jan Steidl
    http://openaltimeter.org

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openaltimeter.desktopapp.flightmodes;

import java.awt.Color;
import java.util.Arrays;
import java.util.Vector;

public class FlightModeSetting 
{
	private Vector<FlightModeRecord> flightModes;
	private String name;
	private float opacity = 0.1f;

    private static final Color[] COLORS = { Color.lightGray, Color.red, Color.green, Color.blue, Color.cyan, Color.magenta, Color.orange, Color.pink, Color.white, Color.yellow };
    private static int colorIndex = 0;
    
	public static Color getNextColor() 
	{
		colorIndex = colorIndex % COLORS.length;
		return COLORS[colorIndex++];
	}
	
	public FlightModeSetting(String name) 
	{
		colorIndex = 0;
		this.name = name;
		this.flightModes = new Vector<FlightModeRecord>();
		this.flightModes.add(new FlightModeRecord(getNextColor(), 1200));
		this.flightModes.add(new FlightModeRecord(getNextColor(), 1500));
		this.flightModes.add(new FlightModeRecord(getNextColor(), 1800));
	}
	
	public void setFlightModes(Vector<FlightModeRecord> flightModes) 
	{
		this.flightModes = flightModes;
		colorIndex = flightModes.size() % COLORS.length;
	}
	
	public Vector<FlightModeRecord> getFlightModes() 
	{
		return flightModes;
	}
	
	public void setName(String name) 
	{
		this.name = name;
	}
	
	public String getName() 
	{
		return name;
	}
	
	public void setOpacity(float opacity) 
	{
		this.opacity = opacity;
	}
	
	public float getOpacity() 
	{
		return opacity;
	}

	@Override
	public String toString() 
	{
		StringBuilder sb = new StringBuilder();
		sb.append(name).append("#");
		sb.append(opacity).append("#");
		
		FlightModeRecord modes[] = getSortedModes();		
		for (FlightModeRecord record : modes) 
		{
			sb.append(record.toString()).append("#");
		}
		
		return sb.toString();
	}
	
	public void setFlightModes(String code) 
	{
		String values[] = code.split("#");
		
		this.name = values[0];
		this.opacity = Float.valueOf(values[1]);
		
		flightModes.removeAllElements();
		
		for (int i = 2; i < values.length; i++)
			flightModes.add(new FlightModeRecord(values[i]));
	}

	public FlightModeRecord[] getSortedModes() 
	{
		FlightModeRecord modes[] = new FlightModeRecord[1]; 
		modes = flightModes.toArray(modes);
		
		Arrays.sort(modes);
		
		return modes;
	}
}
